﻿// Copyright 2012 Stuart Turner
// Licensed under the Apache License, Version 2.0 [http://www.apache.org/licenses/LICENSE-2.0]

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using Complete.Data.Linq;
using Complete.Data.Support;

namespace Complete.Data
{
	public interface ITableDefinition
	{
		string GetTableName();
		Type GetTableType();
		IEnumerable<ColumnMapping> GetColumns();
	}

	public class BasicTableDefinition :ITableDefinition
	{
		private INameMapper mapper;
		private Type tableType;
		public BasicTableDefinition(INameMapper mapper, Type type)
		{
			this.mapper = mapper;
			this.tableType = type;
		}

		public string GetTableName()
		{
			return mapper.GetTableName(tableType);
		}

		public Type GetTableType()
		{
			return tableType;
		}

		public IEnumerable<ColumnMapping> GetColumns()
		{
			return mapper.GetColumns(tableType);
		}
	}

	public class CompleteTable<T> : Query<T>, IUpdateable<T>, ITableDefinition
	{
		private CompleteContext _Context;
		internal CompleteTable(CompleteContext context)
			: base(context.Provider)
		{
			_Context = context;
		}

		public virtual string GetTableName()
		{
			return _Context.NameMapper.GetTableName(typeof(T));
		}

		public Type GetTableType()
		{
			return typeof(T);
		}

		public IEnumerable<ColumnMapping> GetColumns()
		{
			return _Context.NameMapper.GetColumns(typeof(T)).ToList();
		}
	}
}
